package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.Notice;
import cn.anxingongyou.anxin.service.NoticeService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class NoticeController {

    protected static final Logger logger = LoggerFactory.getLogger(NoticeController.class);
    private final NoticeService noticeService;
    @Autowired
    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @Resource
    private RedisUtil redisUtil;

    private static final String  NoticeOfIndex = "notice";

    @ApiOperation("添加公告信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/notice/add",method = RequestMethod.POST)
    public Map addNotice(
            @RequestBody @ApiParam(value = "公告信息", required = true) Notice notice
            ){
        if(redisUtil.hasKey(NoticeOfIndex)){
            redisUtil.del(NoticeOfIndex);
        }
        notice.setCreated(new Date());
        notice.setUpdated(new Date());
        int res = noticeService.insert(notice);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除公告信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/notice/del/{id}",method = RequestMethod.GET)
    public Map delNotice(
            @PathVariable @ApiParam(value = "公告主键", required = true) Long id
    ){
        if(redisUtil.hasKey(NoticeOfIndex)){
            redisUtil.del(NoticeOfIndex);
        }
        int res = noticeService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新公告信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/notice/update",method = RequestMethod.POST)
    public Map updateNotice(
            @RequestBody @ApiParam(value = "公告信息,必传主键", required = true) Notice notice
    ){
        if(redisUtil.hasKey(NoticeOfIndex)){
            redisUtil.del(NoticeOfIndex);
        }
        int res = noticeService.updateByPrimaryKeySelective(notice);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/notice/findByNotice",method = RequestMethod.POST)
    public Map findOneNotice(
            @RequestBody @ApiParam(value = "公告信息", required = true) Notice notice
    ){
        Notice notice1 = noticeService.selectOne(notice);
        if(notice1 != null) {
            return Result.<Notice>instance(200,"获取成功",notice1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取公告信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/notice/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "公告主键", required = true) Long id
    ){
       Notice notice = noticeService.selectOneByPrimaryKey(id);
        if(notice != null) {
            return Result.<Notice>instance(200,"获取成功",notice).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取首页公告信息通过主键，主键id暂时传1")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/notice/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneByIdOfIndex(
            @PathVariable @ApiParam(value = "公告主键", required = true) Long id
    ){
        if(redisUtil.hasKey(NoticeOfIndex)){
            Notice notice = (Notice) redisUtil.get(NoticeOfIndex);
            return Result.<Notice>instance(200,"获取成功",notice).toJsonMap();
        }else {
            Notice notice = noticeService.selectOneByPrimaryKey(id);
            if(notice != null) {
                redisUtil.set(NoticeOfIndex,notice);
                return Result.<Notice>instance(200,"获取成功",notice).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }
    }

    @ApiOperation("获取所有公告")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/notice/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByNotice(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "公告信息", required = true)  Notice notice){
        PageInfo<Notice> pageInfo = noticeService.page(pageNum, pageSize, notice);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }
}
